home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Database / StaticRowsTV / StaticRowsTV.m < prev    next >
Text File  |  1994-02-22  |  6KB  |  216 lines

  1. /* StaticRowsTV.m:
  2.  * You may freely copy, distribute, and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  4.  * fitness for any particular use.
  5.  *
  6.  * Written by: Mai Nguyen, NeXT Developer Support
  7.  *
  8.  */
  9. #import "StaticRowsTV.h"
  10.  
  11. #define INSTALL_MODEL NXLocalizedString("Please install SybaseDemo.dbmodela or OracleDemo.dbmodela in your project.", NULL, "Notify user that the ascii model files must be installed in his project directory.")
  12.  
  13. /* Comment this line out if you want to run an Oracle version */
  14. /* #define    SYBASE_DEMO     1*/
  15.  
  16. @implementation StaticRowsTV
  17. /*
  18. * Miscellaneous initialization tasks: connect to database, initialize
  19. * tableview, set up dbModule. 
  20. */ 
  21. -appDidInit:sender
  22. {
  23.     NXRect viewFrame;
  24.     
  25. #ifdef SYBASE_DEMO    
  26.     /* Notify the user if the database can't be found */
  27.     if (!(dbDatabase = [DBDatabase findDatabaseNamed:"SybaseDemo" 
  28.                     connect:YES])) {
  29.         NXRunAlertPanel(NULL,INSTALL_MODEL, "OK", NULL, NULL);
  30.         return self;
  31.     }
  32. #else
  33.     /* Notify the user if the database can't be found */
  34.     if (!(dbDatabase = [DBDatabase findDatabaseNamed:"OracleDemo" 
  35.                     connect:YES])) {
  36.         NXRunAlertPanel(NULL,INSTALL_MODEL, "OK", NULL, NULL);
  37.         return self;
  38.     }
  39. #endif
  40.  
  41.     [dbDatabase setDelegate:self];
  42.     
  43.         /* Install the tableview into the custom view */
  44.     [[dbTableView getFrame:&viewFrame] free];
  45.     dbTableView = [[DBTableView alloc] initFrame:&viewFrame];
  46.     [[window contentView] addSubview:dbTableView];
  47.     [dbTableView setRowHeadingVisible:YES];
  48.     [dbTableView setColumnHeadingVisible:NO];
  49.     [dbTableView setEditable:YES];
  50.     [dbTableView setHorizScrollerRequired:YES];
  51.     [dbTableView setVertScrollerRequired:YES];
  52.     [dbTableView setDelegate: self];
  53.  
  54. #ifdef SYBASE_DEMO
  55.     rootEntity = [dbDatabase entityNamed:"Author"];
  56. #else
  57.     rootEntity = [dbDatabase entityNamed:"Employee"];
  58. #endif
  59.         /* Must initialize your dbModule */
  60.       dbModule = [[DBModule alloc] initDatabase:dbDatabase 
  61.                             entity:rootEntity];                    
  62.     dbFetchGroup = [dbModule rootFetchGroup];
  63.     [dbFetchGroup setDelegate:self];
  64.      [window disableFlushWindow];
  65.     [self initTableView];
  66.  
  67.             /* Initialize the retrieval order to be ascending order
  68.              */
  69. #ifdef SYBASE_DEMO
  70.     sortProp = [[dbModule entity] propertyNamed:"lastName"];
  71. #else
  72.     sortProp = [[dbModule entity] propertyNamed:"EmployeeName"];
  73. #endif
  74.             
  75.     [[dbFetchGroup recordList] addRetrieveOrder:DB_AscendingOrder for:sortProp];
  76.         
  77.  
  78.     [dbFetchGroup fetchContentsOf:nil usingQualifier:nil];     
  79.     [dbTableView display];
  80.  
  81.     [[window reenableFlushWindow] flushWindow];
  82.     
  83.     return self;
  84. }
  85.  
  86.  
  87. /* In order to replicate the Interface Builder functionality, one has to
  88.  * add an expression to the fetchgroup for each table property.
  89.  */
  90.  
  91. - addTableRow:(const char *)label
  92. {
  93.     id newExpression;
  94.  
  95.     /* allocate a new expression, add it to the FetchGroup and add it */
  96.     /* to the TableView */
  97.     newExpression = [[DBExpression alloc] initForEntity:[dbModule entity]
  98.                 fromDescription:label];
  99.     [dbFetchGroup addExpression:newExpression];
  100.     [dbTableView  addRow:newExpression withTitle:label];
  101.     return self;
  102. }
  103.  
  104. - initTableView
  105. {
  106.     int i, propCount;
  107.     id prop;
  108.  
  109.       propList = [[List alloc] init];
  110.  
  111.     /* Just pick the most important properties for this example */
  112. #ifdef SYBASE_DEMO
  113.     [propList addObject: [rootEntity propertyNamed:"lastName"]];
  114.     [propList addObject: [rootEntity propertyNamed:"firstName"]];
  115.     [propList addObject: [rootEntity propertyNamed:"city"]];
  116.     [propList addObject: [rootEntity propertyNamed:"phone"]];
  117. #else
  118.     [propList addObject: [rootEntity propertyNamed:"EmployeeName"]];
  119.     [propList addObject: [rootEntity propertyNamed:"job"]];
  120.     [propList addObject: [rootEntity propertyNamed:"salary"]];
  121.     [propList addObject: [rootEntity propertyNamed:"hiredate"]];
  122. #endif    
  123.      
  124.     propCount = [propList count];
  125.  
  126.     for (i = 0; i < propCount; i++) {
  127.         prop = [propList objectAt:i];
  128.         [self addTableRow:[prop name]];
  129.         [[dbTableView rowAt:i] setMinSize:30.0];
  130.         }     
  131.  
  132.         /* Make the StaticRowsTV object
  133.          * become the tableview data source.
  134.          */
  135.     [dbTableView setDataSource:self];
  136.  
  137.         /*
  138.          * free property list which is no longer needed
  139.          */
  140.  
  141.     [propList free];
  142.     [window makeKeyAndOrderFront:self];
  143.     return self;
  144. }
  145.  
  146. - showAll:sender
  147. {
  148.     [dbModule fetchAllRecords:sender];
  149.         /* These methods force the tableview to update the display */
  150.     [dbFetchGroup redisplayEverything];
  151.     [dbTableView display];
  152.     return self;
  153. }
  154.  
  155. - save:sender
  156. {
  157.     /* This method is needed to force a save. Otherwise, a Carriage Return
  158.      * is needed before the save actually happens.
  159.      * The typecasting is needed to avoid compiler warnings.
  160.      */
  161.      [(DBTableView *)dbTableView endEditing];
  162.  
  163.     [dbModule saveChanges:sender];
  164.     return self;
  165. }
  166.  
  167. - free
  168. {
  169.     if (dbTableView)
  170.         [dbTableView free];
  171.     return [super free];
  172. }
  173.  
  174. /* DBDatabase delegate methods to log SQL queries  - Useful for debugging */
  175.  
  176. - (BOOL)db:aDb willEvaluateString:(const char*)aString usingBinder:aBinder
  177. {
  178.     fprintf(stderr, "SQL query:%s\n", aString);
  179.     return YES;
  180. }
  181.  
  182. /******************* DBFetchGroup delegate methods ******************/
  183.  
  184. /*
  185.  * This method is called at each SELECT operation. 
  186.  * Restore the proper sort order.
  187.  */
  188. - fetchGroupWillFetch:fetchGroup
  189. {            
  190.     [[dbFetchGroup recordList] addRetrieveOrder:DB_AscendingOrder for:sortProp];
  191.     return self;
  192. }
  193.  
  194.  
  195. /*****************  DBTableView dataSource methods  *****************/
  196.  
  197. - (unsigned)columnCount
  198. {
  199.     return [[dbFetchGroup recordList] count];
  200. }
  201.  
  202.  
  203. - getValueFor:aProperty at:(unsigned)index into:(DBValue*)aValue
  204. {
  205.      return [[dbFetchGroup recordList] getValue:aValue forProperty:aProperty at:index];
  206. }
  207.  
  208. - setValueFor:aProperty at:(unsigned)index from:(DBValue*)aValue
  209. {
  210.      
  211.     [[dbFetchGroup recordList] setValue:aValue forProperty:aProperty at:index];
  212.     return self;
  213. }
  214.  
  215. @end
  216.